package com.fh.mq;

import com.fh.service.cuijimanage.CuijimanageServiceImpl;
import com.fh.service.loantaskimpl.LoanTaskOverServiceImpl;
import com.fh.util.JsonUtil;
import com.rabbitmq.tools.json.JSONUtil;

import net.sf.json.util.JSONUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fh.model.Page;
import com.fh.service.generalview.impl.GeneralViewServiceImpl;
import com.fh.util.PageData;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author dell、
 * @Author LiGuangYi Date: 2017/11/30 10:05
 * @description:
 */
public class StatusQueueListener implements MessageListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(StatusQueueListener.class);
    private static ExecutorService executorService = Executors.newCachedThreadPool();//线程池

    @Autowired
    private CuijimanageServiceImpl cuijimanageServiceImpl;
    @Autowired
    private LoanTaskOverServiceImpl loanTaskOverServiceImpl;

    @Override
    public void onMessage(Message message) {
        byte[] body = message.getBody();
         String msg = null;
         try {
            msg = new String(body, "UTF-8");
            LOGGER.info("接收到队列collectionStatusQueue的mq==>{}", msg);
            if (StringUtils.isEmpty(msg)) {
                LOGGER.info("获取今日现金的MQ数据为空");
            }
            JSONObject jsonMsg = JSONObject.parseObject(msg);
            Map<String, Object> map = new HashMap<String, Object>();
            map = jsonMsg.getJSONObject("data");
            Integer repayType = (Integer) map.get("repayType");//1 为全部还清 0为部分还款
            final String str = msg;
             executorService.submit(new Runnable() {
                 @Override
                 public void run() {
                     LOGGER.info("当前线程==>{}",Thread.currentThread().getName());
                     LOGGER.info("当前线程数目==>{}", Thread.activeCount());
                     int threadCount = ((ThreadPoolExecutor)executorService).getActiveCount();
                     LOGGER.info("当前线程数目1==>{}", threadCount);
                     cuijimanageServiceImpl.doMqMessage(str);
                     if (1 == repayType) {
                         loanTaskOverServiceImpl.getOverCashMsg(str);//获取结案MQ信息 
                     } 
                 }
            });
        } catch (Exception e) {
             LOGGER.error("StatusQueueListener的mq报错==>{}",e);
        }
    }
}
